package com.plugin.planilha;

import java.util.HashMap;
import java.util.Map;

import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

public class AnaliseVectorSheet implements ISheet{

	private WritableWorkbook workbook;
	private HashMap<String, Map<String, Integer[]>> map;
	private static WritableSheet metricaVectortWS;

	public AnaliseVectorSheet(WritableWorkbook w,
			HashMap<String, Map<String, Integer[]>> map) {
		workbook = w;
		this.map = map;
		metricaVectortWS = workbook.createSheet("Metodos Vector synchronized", 1);
	}
	
	@Override
	public void criaCabecalho(WritableCellFormat cf)
			throws RowsExceededException, WriteException {
		
		Label label = new Label(0, 0, "Variavel", cf);
		metricaVectortWS.addCell(label);
		
		label = new Label(1, 0, "Numero de metodos dentro do bloco synchronized", cf);
		metricaVectortWS.addCell(label);
		
		label = new Label(2, 0, "Numero de metodos fora do bloco synchronized", cf);
		metricaVectortWS.addCell(label);

		label = new Label(4, 0, "Total de variaveis que sincronizam seus metodos", cf);
		metricaVectortWS.addCell(label);
		
		label = new Label(5, 0, "Total de variaveis que nao sincronizam seus metodos", cf);
		metricaVectortWS.addCell(label);
		
		label = new Label(6, 0, "Taxa das variaveis que nao sincronizam seus metodos", cf);
		metricaVectortWS.addCell(label);
	}

	@Override
	public void addDados() throws RowsExceededException, WriteException {
		Label label;
		jxl.write.Number n;
		int indice = 1;
		
		double total_variaveis_synchronized = 0, total_variaveis_not_synchronized = 0;
		for (String chave : map.keySet()) {
			if (map.get(chave).size() > 0) {
				for (String variavel : map.get(chave).keySet()) {
					int num_metodos_dentro = map.get(chave).get(variavel)[0];
					int num_metodos_fora = map.get(chave).get(variavel)[1];
					boolean uso_de_synchronized = (num_metodos_dentro > 0) ? true: false;
					
					label = new Label(0, indice, variavel);
					metricaVectortWS.addCell(label);
					
					n = new jxl.write.Number(1, indice, (int) num_metodos_dentro);
					metricaVectortWS.addCell(n);
					
					n = new jxl.write.Number(2, indice, (int) num_metodos_fora);
					metricaVectortWS.addCell(n);
					
					if (uso_de_synchronized)
						total_variaveis_synchronized++;
					else
						total_variaveis_not_synchronized++;
					
					indice++;
				}
			}
		}
		double round_total=0;
		if((total_variaveis_not_synchronized + total_variaveis_synchronized)!=0){
		double total = (100 * total_variaveis_not_synchronized)
				/ (total_variaveis_not_synchronized + total_variaveis_synchronized);
		round_total = (double) Math.round(total * 1000) / 1000;
		}
		
		n = new jxl.write.Number(4, 1, (int) total_variaveis_synchronized);
		metricaVectortWS.addCell(n);
		
		n = new jxl.write.Number(5, 1, (int) total_variaveis_not_synchronized);
		metricaVectortWS.addCell(n);

		n = new jxl.write.Number(6, 1, round_total);
		metricaVectortWS.addCell(n);
	}

}
